# 不执行逻辑，目的是为了不让 from apps.crawler_platform.core_callback import *
# 被编辑器认为没有使用，快捷清除时就不会被清除

from re_common.baselibrary.utils.core.mdeprecated import try_except2_async

from apps.crawler_platform.core_callback import null
from apps.crawler_platform.core_callback.core_callback import sql_model_callback
from apps.crawler_platform.core_callback.err_callback import all_client_back_callback
from apps.crawler_platform.core_platform.core_g import SQLTable
from apps.crawler_platform.core_platform.core_nosql import NoSqlUtil
from apps.crawler_platform.core_platform.core_sql import CoreSqlMixin
from apps.crawler_platform.core_platform.g_model import DownModel, HtmlModel, OtherArticleModel, T2, T1, \
    OtherArticleSpeModel
from apps.crawler_platform.other_platform.base_other_step import BaseOtherStep, BaseOtherItems

STATICA_HACK = False
# globals()['kcah_acitats'[::-1].upper()] = False
if STATICA_HACK:  # pragma: no cover
    null()


class ArticleParaItems(BaseOtherItems):

    def __init__(self):
        super(ArticleParaItems, self).__init__()
        self.page_index = 1
        self.rawid = ""

    def get_pkm(self):
        """
        获取主键
        :return:
        """
        return {"task_name": self.task_name,
                "task_tag": self.task_tag,
                "rawid": self.rawid}

    def set_downmodel(self):
        if self.task_name in ["cnkithesis", "cnkicdfdthesis", "cnkiconference", "cnkiipfdconference"]:
            self.down_model = DownModel[T2](down_dict={})
        elif self.task_name in ["cnkiimage", "wanfangthesis", "wanfangconference", "wanfangunconference"]:
            self.down_model = DownModel[T1](down_dict={})
        else:
            self.down_model = DownModel[HtmlModel](down_dict={})


class OtherArticleDownStep(BaseOtherStep):

    def __init__(self, request, inputs):
        super().__init__()
        self.err_tag = "otherarticle"
        self.pm = ArticleParaItems()
        self.pm.set_downmodel()
        # api传入数据
        self.input = inputs
        self.pm.task_name = inputs.data.task_name
        self.pm.task_tag = inputs.data.task_tag
        self.pm.rawid = inputs.data.rawid
        self.pm.Id = self.pm.rawid

        # fastapi 自带的 Request
        self.request = request
        # 这里可以让callback使用到pm变量
        self.request.state.pm = self.pm
        self.pm.redis = request.app.state.redis
        # 每一步的文字说明
        self.step_info = ""
        # 解析字段字典
        self.para_dicts = {}

        # 读取sql的model
        self.sql_model: OtherArticleModel
        # 请继承类设置该参数
        self.task_table = SQLTable.other_article

    def set_table(self, *args, **kwargs):
        inputs = args[0]
        task_tag = inputs.data.task_tag
        self.sql_model = OtherArticleModel
        if task_tag in ["cnkithesisarticle", "cnkicdfdthesisarticle", "cnkiconferencearticle",
                        "cnkiipfdconferencearticle", "wanfangthesis_search_article",
                        "wanfangconference_search_article", "wanfangunconference_search_article",
                        "baiduxuearticle"]:
            self.task_table = SQLTable.other_article_spe
            self.sql_model = OtherArticleSpeModel
        return self.task_table

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def deal_html(self):
        return await super().deal_html(self.sql_model)

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def save_sql(self, type="", code=""):
        """
        与关系型sql的处理 包括存储下一级的sql以及本级sql的状态和字段更新
        """
        sql_next_table = ''
        bools, msg = await super().save_sql(self.task_table, sql_next_table, type, code)
        return bools, msg

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def get_sql(self):
        if self.task_table == SQLTable.other_article_spe:
            row = await CoreSqlMixin.get_otherarticle_spe(self.pm.task_name, self.pm.task_tag, self.pm.rawid)
            if row:
                row = sql_model_callback(row)
                self.pm.sql_model = OtherArticleSpeModel.parse_obj(row)
                return True, row
            else:
                return False, {"err_msg": "qk list sql查询不存在;" + self.pm.task_tag + "," + self.pm.rawid}
        else:
            row = await CoreSqlMixin.get_otherarticle(self.pm.task_name, self.pm.task_tag, self.pm.rawid)
            if row:
                row = sql_model_callback(row)
                self.pm.sql_model = OtherArticleModel.parse_obj(row)
                return True, row
            else:
                return False, {"err_msg": "qk list sql查询不存在;" + self.pm.task_tag + "," + self.pm.rawid}

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def save_nosql_html(self):
        NoSqlUtil.set_is_his(self.pm, "True")
        bools, dicts = await super().save_nosql_html()
        return bools, dicts
